import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    one_oneusermessage: {},
    frindesLists: [],
    myuserXx: {},
    rooms: [],
    roomMessage: {}
  },
  getters: {
    one_oneusermessagefiltere: (state) => (id) => {
      return state.one_oneusermessage[String(id)]
    },
    messagekey: (state) => () => {
      let list = []
      if (Object.keys(state.one_oneusermessage).length !== 0) {
        list = Object.keys(state.one_oneusermessage).map(item => { return item })
      }
      return list
    },
    roomMessagekey:(state)=>()=>{
      let list = []
      if (Object.keys(state.roomMessage).length !== 0) {
        list = Object.keys(state.roomMessage).map(item => { return item })
      }
      return list
    },
    one_onefriendlastmessageXx: (state) => (id) => {
      return state.one_oneusermessage[String(id)][state.one_oneusermessage[String(id)].length - 1]
    },
    userXxget: (state) => (id) => {
      if (state.frindesLists.length !== 0) {
        const index = state.frindesLists.findIndex(item => Number(item.userid) === Number(id))
        if (index !== -1) {
          return state.frindesLists[index]
        } else {
          return {}
        }
      } else {
        return {}
      }
    },
    rooXxget:(state)=>(id)=>{
      const index=state.rooms.findIndex(item=>Number(item.roomids)===Number(id))
      if(index!==-1){
        return state.rooms[index]
      }else{
        return {}
      }
      
    },
    LastRoomMessage:(state)=>(id)=>{
     return state.roomMessage[Number(id)][state.roomMessage[Number(id)].length - 1]
    },
    roomIdMessages:(state)=>(id)=>{
     return state.roomMessage[Number(id)]
    }
  },
  mutations: {
    updateone_oneusermessage(state, message) {
      state.one_oneusermessage = message;
    },
    updatemessageonetoone(state, data) {
      console.log(data);
      if (state.one_oneusermessage[Number(data.type)]) {
        state.one_oneusermessage[Number(data.type)].push(data.parmas)
      } else {
        state.one_oneusermessage[Number(data.type)] = []
        state.one_oneusermessage[Number(data.type)].push(data.parmas)
      }
    },
    updataRoomMessage(state, data){
      // console.log(data);
      if(state.roomMessage[Number(data.type)]){
        state.roomMessage[Number(data.type)].push(data.parmas)
      }else{
        state.roomMessage[Number(data.type)] = []
        state.roomMessage[Number(data.type)].push(data.parmas)
      }
    },
    update_frindesLists(state, frindesLists) {
      state.frindesLists = frindesLists;
    },
    setmyuserXx(state, data) {
      state.myuserXx = data
    },
    roomslist(state, data) {
      state.rooms = data
    },
    roomMessage(state, data) {
      state.roomMessage = data
    }
  },
  actions: {
  },
  modules: {
  }
})
